/**
 * 
 */
package com.nari.heatshare.dao;

import java.util.Calendar;
import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.heatshare.model.IAssignMeterDataFile;
import com.nari.platform.date.CalendarUtils;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-18
 */
public class AssignMeterDataFileDaoImpl extends GenericDaoImpl<IAssignMeterDataFile, String> implements
		IAssignMeterDataFileDao {

	/**
	 * 分页查询分配表数据文件列表
	 * @param fileName 数据文件名(可选)
	 * @param startDate 起始上传日期(可选)
	 * @param endDate 终止上传日期(可选)
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-1
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<IAssignMeterDataFile> queryDataFilePage(String fileName, Date startDate, 
			Date endDate, PageCond pageCond) throws Exception {
		Criteria listCriteria = this.getCurrentSession().createCriteria(IAssignMeterDataFile.class);
		Criteria countCriteria = this.getCurrentSession().createCriteria(IAssignMeterDataFile.class);
		//分配表数据文件名
		if (fileName!=null && !"".equals(fileName.trim())) {
			listCriteria.add(Restrictions.ilike("fileName", fileName.trim().toLowerCase(), MatchMode.ANYWHERE));
			countCriteria.add(Restrictions.ilike("fileName", fileName.trim().toLowerCase(), MatchMode.ANYWHERE));
		}
		//上传日期
		if(startDate!=null && endDate==null){
			Calendar startCal = CalendarUtils.getCalFromDateStr(CalendarUtils.parseDateToString(startDate));
			listCriteria.add(Restrictions.ge("uploadDate", CalendarUtils.getMinCalBasedSrcCal(startCal)));
			countCriteria.add(Restrictions.ge("uploadDate", CalendarUtils.getMinCalBasedSrcCal(startCal)));
		}else if(startDate==null && endDate!=null){
			Calendar edCal = CalendarUtils.getCalFromDateStr(CalendarUtils.parseDateToString(endDate));
			listCriteria.add(Restrictions.le("uploadDate", CalendarUtils.getMaxCalBasedSrcCal(edCal)));
			countCriteria.add(Restrictions.le("uploadDate", CalendarUtils.getMaxCalBasedSrcCal(edCal)));
		}else if(startDate!=null && endDate!=null){
			Calendar startCal = CalendarUtils.getCalFromDateStr(CalendarUtils.parseDateToString(startDate));
			Calendar edCal = CalendarUtils.getCalFromDateStr(CalendarUtils.parseDateToString(endDate));
			listCriteria.add(Restrictions.between("uploadDate", 
					CalendarUtils.getMinCalBasedSrcCal(startCal), 
					CalendarUtils.getMaxCalBasedSrcCal(edCal)));
			countCriteria.add(Restrictions.between("uploadDate", 
					CalendarUtils.getMinCalBasedSrcCal(startCal), 
					CalendarUtils.getMaxCalBasedSrcCal(edCal)));
		}
		//统计结果集个数
		int total = ((Long)countCriteria.setProjection(Projections.rowCount()).list().get(0)).intValue();
		//结果集排序
		listCriteria.addOrder(Order.desc("uploadDate")).addOrder(Order.asc("fileName"));
		//结果集分页
		listCriteria.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
		//构造返回类型
		pageCond.setTotal(total);
		ResultWithPageCond<IAssignMeterDataFile> dataFilePage = new ResultWithPageCond<IAssignMeterDataFile>();
		dataFilePage.setList(listCriteria.list());
		dataFilePage.setPageCond(pageCond);
		return dataFilePage;
	}
}
